What is parse-diff?
The `parse-diff` npm package is a utility for parsing unified diffs. It allows you to analyze and manipulate diff data, which is useful for applications that need to process changes between file versions, such as code review tools, version control systems, and more.
What are parse-diff's main functionalities?
Parse a diff string
This feature allows you to parse a unified diff string into a structured format. The code sample demonstrates how to parse a diff string and log the resulting file changes.
const parse = require('parse-diff');
const diffString = `diff --git a/file1.txt b/file1.txt
index 83db48f..f735c2d 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,3 +1,3 @@
-Hello World
+Hello parse-diff
This is a test file.
It has multiple lines.`;
const files = parse(diffString);
console.log(files);
Access file changes
This feature allows you to access detailed information about file changes, including chunks and individual changes. The code sample demonstrates how to iterate through the parsed diff data and log the file, chunk, and change details.
const parse = require('parse-diff');
const diffString = `diff --git a/file1.txt b/file1.txt
index 83db48f..f735c2d 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,3 +1,3 @@
-Hello World
+Hello parse-diff
This is a test file.
It has multiple lines.`;
const files = parse(diffString);
files.forEach(file => {
console.log(`File: ${file.to}`);
file.chunks.forEach(chunk => {
console.log(`Chunk: ${chunk.content}`);
chunk.changes.forEach(change => {
console.log(`Change: ${change.content}`);
});
});
});
Other packages similar to parse-diff
diff
The `diff` package provides a set of tools to compare strings, arrays, and objects, and generate diffs. It is more general-purpose compared to `parse-diff`, which is specifically designed for parsing unified diff strings. `diff` can be used for a wider range of diffing tasks, but may require more effort to handle unified diff formats.
diff2html
The `diff2html` package converts unified diff data into HTML. It is useful for visualizing diffs in a web application. While `diff2html` focuses on rendering diffs as HTML, `parse-diff` is more about parsing and analyzing the diff data. They can be used together, with `parse-diff` handling the parsing and `diff2html` handling the rendering.
git-diff-parser
The `git-diff-parser` package is another tool for parsing git diff output. It provides similar functionality to `parse-diff`, allowing you to parse and analyze diff data. However, `git-diff-parser` is specifically tailored for git diffs, whereas `parse-diff` can handle a broader range of unified diff formats.
![Total downloads](https://img.shields.io/npm/dt/parse-diff.svg)
![NPM](https://nodei.co/npm/parse-diff.png?downloads=true&stars=true)
parse-diff
Simple unified diff parser for JavaScript
JavaScript Usage Example
var parse = require('parse-diff');
var diff = '';
var files = parse(diff);
console.log(files.length);
files.forEach(function(file) {
console.log(file.chunks.length);
console.log(file.chunks[0].changes.length)
console.log(file.deletions);
console.log(file.additions);
});